package com.wayhome.srpingbootmybatis.controller.excel;

import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@RestController
@RequestMapping(value = "/opt/exportTemplateFile")
public class TemplateExportController {

    @Autowired
    private ResourceLoader resourceLoader;

    @PostMapping("/exportByResourceFile")
    public void exportByResourceFile(HttpServletResponse response) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=template.xlsx");
        try {
            InputStream inputStream = this.getClass().getResourceAsStream("/template/上传处罚记录.xlsx");
            OutputStream outputStream = response.getOutputStream();
            assert inputStream != null;
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
        } catch (IOException e) {
            throw new RuntimeException("文件下载失败：" + e.getMessage());
        }
    }
}
